//
//  ByPersonalCenterVC.m
//  caipiao_ios
//
//  Created by mc on 2018/7/3.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "ByPersonalCenterVC.h"
#import "ByMyHeadView.h"
#define CELL @"Cell0"
#import "ByRechargeVC.h"
#import "ByWithdrawVC.h"
#import "BySetVC.h"
#import "ByMoneyChangeDetailVC.h"
#import "ByUserDataVC.h"
#import "ByUserPerfectDataVC.h"
#import "ByFeedbackVC.h"
#import "ByNewsCenterVC.h"
#import "ByContactCustomerVC.h"
#import "ByBetRecordVC.h"
#import "ByAboutUsVC.h"
#import "ByChaseNumberManageVC.h"
#import "ByRecommendListVC.h"
#import "ByMyFocusVC.h"
#import "ByRecommendCodeVC.h"

@interface ByPersonalCenterVC ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>{
    
    // 视图
    UICollectionView *_collectionView;
}

@property (nonatomic, strong) ByMyHeadView *headView;

@end

@implementation ByPersonalCenterVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        [self loadData];
    }];
    _collectionView.mj_header = header;
    
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.hidden = YES;
    
    [self performSelector:@selector(reloadView) withObject:nil afterDelay:0.2];
   
}


- (void)reloadView{
    
    if (BY_USER && BY_USER.uid) {
        [self loadData];
        
    }else{
        
        self.headView.nameLabel.text = @"未登录";
        self.headView.iconView.image = HEAD_ICOM_IMAGE;
        self.headView.leftView.valueLabel.text = [NSString stringWithFormat:@"%@元", @"0"];
        self.headView.rightView.valueLabel.text = [NSString stringWithFormat:@"%@元", @"0"];
        
        self.headView.focusLabel.text = [NSString stringWithFormat:@"关注:"];
        self.headView.fansLabel.text = [NSString stringWithFormat:@"粉丝:"];
    }
    
    
  
}

- (void)loadData{
    
    
    if (!BY_USER) {
        [_collectionView.mj_header endRefreshing];
        return;
    }
    
    self.headView.leftView.valueLabel.text = [NSString stringWithFormat:@"%.2f元", BY_USER.user_money.floatValue];
    self.headView.nameLabel.text = BY_USER.nickname;
    [self.headView.iconView sd_setImageWithURL:[NSURL URLWithString:BY_USER.head_pic] placeholderImage:HEAD_ICOM_IMAGE];
    
    
    [ByMoneyApi userMoneyFollowSuccess:^(ByNetBaseResult *result) {
        [_collectionView.mj_header endRefreshing];
        NSLog(@"查询 = %@", result.data);
        NSDictionary *dic = result.data[@"data"];
        NSString *fensi = dic[@"fensi"];
        NSString *guanzhu = dic[@"guanzhu"];
        NSString *keyong = dic[@"keyong"];
        NSString *tixian = dic[@"tixian"];
        NSString *invitation_code = dic[@"invitation_code"];
        ByModelUser *user = BY_USER;
        user.withdrawals_money = tixian;
        user.user_money = keyong;
        user.invitation_code = invitation_code;
        [ByCacheUser setUserInfo:user];
        self.headView.rightView.valueLabel.text = [NSString stringWithFormat:@"%.2f元", tixian.floatValue];
        self.headView.focusLabel.text = [NSString stringWithFormat:@"关注：%@",guanzhu];
        self.headView.fansLabel.text = [NSString stringWithFormat:@"粉丝：%@",fensi];
        self.headView.leftView.valueLabel.text = [NSString stringWithFormat:@"%.2f元", keyong.floatValue];
        
        
    } failure:^(ByNetBaseResult *result) {
        [_collectionView.mj_header endRefreshing];
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
    
    [ByApiOther paySupportSuccess:^(ByNetBaseResult *result){
        
        NSLog(@"支付方式获取 = %@", result.data);
        NSArray *array = result.data[@"data"];
        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSData * data =  [NSKeyedArchiver archivedDataWithRootObject:array];
        [userDefaults  setObject:data forKey:KEY_Pay_Style];//存储设备列表
        [userDefaults synchronize];
        
    } failure:^(ByNetBaseResult *result) {
        
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];
    
}




- (void)initViews{
    self.view.backgroundColor = rgba(246, 246, 246, 1);
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49) collectionViewLayout:layout];
    [self.view addSubview:_collectionView];
    adjustsScrollViewInsets_NO(_collectionView, self);
    // 设置代理和数据源方法
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    self.view.backgroundColor = [UIColor whiteColor];
    _collectionView.backgroundColor = rgba(246, 246, 246, 1);
    _collectionView.scrollEnabled = YES;
    _collectionView.userInteractionEnabled = YES;
    _collectionView.alwaysBounceVertical = YES;
    //给集合视图注册一个类型的cell
    [_collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:CELL];
    [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HEADERVIEW"];
}


#pragma mark - collectionView 代理
// 定义展示的UICollectionViewCell的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
// 定义展示的Section的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 11;
}
// 每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:CELL forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    CGFloat width = 27;
    UIImageView *imageView = [[UIImageView alloc] init];
    [cell.contentView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(cell.contentView).offset(20);
        make.centerX.equalTo(cell.contentView).offset(0);
        make.size.mas_equalTo(CGSizeMake(width, width));
    }];
    NSArray *imagesAry = @[@"chongzhi",@"tixian",@"touzhujilu",@"zhanghumingxi",@"xiaoxizhongxin",@"zhuihaoguanli",@"kefu",@"yijianfankui",@"guanyuwomen-1",@"gendanjilu跟单记录",@"chakantuijianma"];
    imageView.image = [UIImage imageNamed:imagesAry[indexPath.row]];
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [cell.contentView addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(cell.contentView).offset(0);
        make.top.equalTo(imageView.mas_bottom).offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3.0, 20));
    }];
    nameLabel.textColor = [UIColor grayColor];
    nameLabel.font = FONT(14);
    nameLabel.textAlignment = NSTextAlignmentCenter;
    
    NSArray *dataArray = @[@"充值",@"提现",@"投注记录",@"账户明细",@"消息中心",@"追号管理",@"联系客服",@"意见反馈",@"关于我们",@"跟单记录",@"推荐码"];
    nameLabel.text = dataArray[indexPath.row];
    return cell;
    
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 1.5;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1.5;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return  CGSizeMake( (SCREEN_WIDTH - 3) / 3.0, 20 + 50 + 10 + 20 + 15);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(SCREEN_WIDTH, 250);
    
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if ( kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HEADERVIEW" forIndexPath:indexPath];
        headerView.backgroundColor = rgba(246, 246, 246, 1);
        [headerView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        ByMyHeadView *view = [[ByMyHeadView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 240)];
        self.headView = view;
        view.setBlock = ^(){
            
            if (!BY_USER) {
                ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
            
            BySetVC *vc = [[BySetVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
//            ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
//            [self by_normalPush:vc hasNavi:YES];
            
        };
        
        view.fansBlock = ^(){
            if (!BY_USER) {
                ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
            
//            ByMyFocusVC *vc = [[ByMyFocusVC alloc] init];
//            vc.isFansList = YES;
//            [self by_normalPush:vc hasNavi:YES];
        };
        
        view.focusBlock = ^(){
            if (!BY_USER) {
                ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
            
            ByMyFocusVC *vc = [[ByMyFocusVC alloc] init];
            vc.isFansList = NO;
            [self by_normalPush:vc hasNavi:YES];
            
        };
        
       
        view.tapViewBlock = ^(){
            
            if (!BY_USER) {
                ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
                [self by_normalPush:vc hasNavi:YES];
            }
            
            ByUserDataVC *vc = [[ByUserDataVC alloc] init];
            [self by_normalPush:vc hasNavi:YES];
            
        };
        
        [headerView addSubview:view];
        return headerView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!BY_USER) {
        ByPhoneLoginVC *vc = [[ByPhoneLoginVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        return;
    }

    if (indexPath.row == 0) {
        ByRechargeVC *vc = [[ByRechargeVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }else if (indexPath.row == 1){
        ByWithdrawVC *vc = [[ByWithdrawVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }else if (indexPath.row == 2){
        
        ByBetRecordVC *vc = [[ByBetRecordVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (indexPath.row == 3){
        ByMoneyChangeDetailVC *vc = [[ByMoneyChangeDetailVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }else if (indexPath.row == 4){
       
        ByNewsCenterVC *vc = [[ByNewsCenterVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (indexPath.row == 5){
        
        ByChaseNumberManageVC *vc = [[ByChaseNumberManageVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (indexPath.row == 6){
        
        
        
        ByContactCustomerVC *vc = [[ByContactCustomerVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
        
    }else if (indexPath.row == 7){
        
        
        ByFeedbackVC *vc = [[ByFeedbackVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
        
    }else if (indexPath.row == 8){
        
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:[NSString stringWithFormat:@"%@/Wanfajiesao/about?type=0",BOYE_WEB_BASE_URL]];
        vc.titleString = @"关于我们";
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (indexPath.row == 9){
        ByRecommendListVC *vc = [[ByRecommendListVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
        
    }else if (indexPath.row == 10){
        
        if (BY_USER.invitation_code.length == 0) {
            [ByCoreToolAlert showOnlyText:@"暂无推荐码"];
            return;
        }else{
            [ByCoreToolAlert showOnlyText:BY_USER.invitation_code];
            return;
        }
        
//        ByRecommendCodeVC *vc  = [[ByRecommendCodeVC alloc] init];
//        [self by_normalPush:vc hasNavi:YES];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
